package com.feihong.yw189.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.feihong.yw189.R;
import com.feihong.yw189.activity.kaika.TaoCanDetailActivity;
import com.feihong.yw189.adapter.holder.SpeedHourHolder;
import com.feihong.yw189.model.Kaika.KuanDaiTcInfo;

import java.util.ArrayList;
import java.util.List;

/**
 * 宽带套餐数据适配器
 * 2017/12/12.
 */

public class ProjectAdapter extends RecyclerView.Adapter<SpeedHourHolder> {
    private int currentIndex = -1;

    private List<KuanDaiTcInfo.DataBean.ListBean.SpecListBean> list = new ArrayList<>();
    private LayoutInflater mInflater;
    private Context mContext;
    private Activity activity;

    private ArrayList<String> taocanmingcheng;
//    private int count; // 判断一证五户所需

    public ProjectAdapter(Context context) {
        this.mContext = context;
        this.activity = (Activity) context;
//        this.count = count;
//        Log.e("项目异常", "------ProjectAdapter------" + count);

        mInflater = LayoutInflater.from(context);
    }

    public void setList(ArrayList<String> taocanmingcheng, List<KuanDaiTcInfo.DataBean.ListBean.SpecListBean> list) {
        this.list = list;
        if (this.list == null) {
            this.list = new ArrayList<>();
        }
        this.taocanmingcheng = taocanmingcheng;
        notifyDataSetChanged();
    }


    public OnItemClickListener mOnItemClickListener;

    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    public void setOnItemClickListener(OnItemClickListener mOnItemClickLitener) {
        this.mOnItemClickListener = mOnItemClickLitener;
    }

    @Override
    public SpeedHourHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = mInflater.inflate(R.layout.item_kdtc, parent, false);

        SpeedHourHolder holder = new SpeedHourHolder(view);
        holder.tv_kd = (TextView) view.findViewById(R.id.tv_kd);
        holder.tv_add = (TextView) view.findViewById(R.id.tv_add);
        holder.tv_tc = (TextView) view.findViewById(R.id.tv_tc);
        holder.tv_gh = (TextView) view.findViewById(R.id.tv_gh);
        holder.tv_detail = (TextView) view.findViewById(R.id.tv_detail);
        holder.speedView = (LinearLayout) view.findViewById(R.id.speed_view);
        holder.rl_detail = (RelativeLayout) view.findViewById(R.id.rl_detail);
        return holder;
    }

    @Override
    public void onBindViewHolder(final SpeedHourHolder holder, final int position) {


        holder.tv_kd.setText(taocanmingcheng.get(position));
        int is_tel = list.get(position).getSystem_spec_info().getShow_parms().getIs_tel();

//        if (count + is_tel > 5) {
//            holder.tv_add.setText("套餐不可选");
//        } else {
//            holder.tv_add.setText("套餐可选");
//        }
        holder.tv_add.setText("全家随便花");

        holder.tv_tc.setText(list.get(position).getPrice());
        holder.tv_gh.setText(list.get(position).getSpec_name());
        if (currentIndex == position) {
            holder.speedView.setScaleX(1.1f);
            holder.speedView.setScaleY(1.1f);
        } else {
            holder.speedView.setScaleX(1f);
            holder.speedView.setScaleY(1f);
        }
        holder.speedView.setTag(position);
        holder.speedView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mOnItemClickListener != null) {
                    int pos = -1;
                    try {
                        pos = Integer.parseInt(view.getTag().toString());
                    } catch (Exception ex) {
                        Log.e("测试", "onClick: ", ex);
                    }
                    if (pos < 0) return;
                    currentIndex = pos;
                    notifyDataSetChanged();
                    mOnItemClickListener.onItemClick(holder.speedView, pos);
                }
            }
        });
        holder.tv_detail.setText("详\n情");
        holder.rl_detail.setTag(position);
        holder.rl_detail.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnItemClickListener != null) {
                    int pos = -1;
                    try {
                        pos = Integer.parseInt(v.getTag().toString());
                    } catch (Exception ex) {
                        Log.e("测试", "onClick: ", ex);
                    }
                    if (pos < 0) return;
                    // Log.e("详情", "onClick------------详情-----------" + list.get(pos).getSystem_spec_id());
                    Intent mIntent = new Intent();
                    mIntent.setClass(activity, TaoCanDetailActivity.class);
                    mIntent.putExtra("system_spec_id", list.get(pos).getSystem_spec_id());
                    activity.startActivity(mIntent);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        if (list.size() > 0) {
            return list.size();
        } else {
            return 0;
        }
    }
}
